package net.towee.client.widgets;

import com.google.gwt.animation.client.Animation;
import com.google.gwt.user.client.ui.Widget;

public class OpacityAnimation extends Animation {
	private boolean showed = false;
	private final Widget widget;
	private int height;

	public OpacityAnimation(Widget w) {
		this(w, false, 0);
	}
	public OpacityAnimation(Widget w, boolean showed) {
		this(w, showed, 0);
	}
	public OpacityAnimation(Widget w, boolean showed, int height) {
		this.widget = w;
		this.showed = showed;
		this.height = height;
		
		
		w.getElement().getStyle().setProperty("overflow", "hidden");
		if(showed)
			w.getElement().getStyle().setProperty("display", "block");
		else
			w.getElement().getStyle().setProperty("display", "none");
	}
	
	@Override
	public void onComplete() {
		super.onComplete();
		
		if(showed)
			widget.getElement().getStyle().setProperty("display", "none");
		
		showed = !showed;
	}
	
	@Override
	public void onStart() {
		super.onStart();
		if(!showed) {
			widget.getElement().getStyle().setProperty("display", "block");
		}
	}
	
	@Override
	protected void onUpdate(double progress) {
		progress = (showed)?1-progress:progress;
		widget.getElement().getStyle().setProperty("opacity", progress+"");
		widget.getElement().getStyle().setProperty("height", (progress*height)+"px");
	}

}
